// Copyright (c) 2024, lanlian and contributors
// For license information, please see license.txt

frappe.ui.form.on("ERP Sync Config", {
    onload:function(frm)
	{
		frappe.db.get_doc("DocType",frm.doc.entity_doctype).then((data)=>
		{
			let sub_table_doc_name_list = data.fields.filter(function(field){return field.fieldtype=="Table"}).map(function(field){return field.options});
			frm.set_query('table_name', 'entry_fields', () => {
				return {
					filters: {name: ['in', sub_table_doc_name_list]}
				}
			})

		});
	},
	refresh(frm) {
        if (frm.doc.form_id === "BD_Supplier") {
			frm.add_custom_button('同步供应商', () => {
				frm.call({
					method: 'ems.equipment_managerment_system.doctype.server_config.server_config.sync_supplier_from_erp',
					freeze: true,
					freeze_message: __("running..."),
					callback: function(r) {
						frappe.msgprint(__('Sucess'))
					}
				})
			});
		}
        if (frm.doc.form_id === "BD_UNIT") {
			frm.add_custom_button('同步计量单位', () => {
				frm.call({
					method: 'ems.equipment_managerment_system.doctype.server_config.server_config.sync_uom_from_erp',
					freeze: true,
					freeze_message: __("running..."),
					callback: function(r) {
						frappe.msgprint(__('Sucess'))
					}
				})
			});
		}
        if (frm.doc.form_id === "BD_MATERIAL") {
			frm.add_custom_button('同步物料主数据', () => {
				frappe.confirm('为确保同步成功请先执行计量单位同步!',
				() => {
					frm.call({
						method: 'ems.equipment_managerment_system.doctype.server_config.server_config.sync_material_from_erp',
						freeze: true,
						freeze_message: __("running..."),
						callback: function(r) {
							frappe.msgprint(__('Sucess'))
						}
					})
				}, () => { // action to perform if No is selected
				})
			});
		}
	},
    entity_doctype:function(frm)
	{
		frappe.db.get_doc("DocType",frm.doc.entity_doctype).then((data)=>{
			current_entity_doc = data;
			frm.doc.entity_fields = [];
			frm.doc.entry_fields = [];
			console.log(data.fields)
			data.fields.forEach(function(field){
				if(filter_field_type_list.indexOf(field.fieldtype)!= -1 || filter_field_name_list.indexOf(field.fieldname)!=-1)
				{
					return;
				}
				frm.add_child("entity_fields",{"field_name":field.fieldname,"target_field":field.fieldname})
			});

			let sub_table_doc_name_list = data.fields.filter(function(field){return field.fieldtype=="Table"}).map(function(field){return field.options});
			frm.set_query('table_name', 'entry_fields', () => {
				return {
					filters: {name: ['in', sub_table_doc_name_list]}
				}
			})

			sub_table_doc_name_list.forEach(function(sub_table_doc_name){
				console.log(sub_table_doc_name)
				frappe.db.get_doc("DocType",sub_table_doc_name).then(function(data){
					console.log("11:",sub_table_doc_name)
					data.fields.forEach(function(field){
						if(filter_field_type_list.indexOf(field.fieldtype)!= -1 || filter_field_name_list.indexOf(field.fieldname)!=-1)
						{
							return;
						}
						frm.add_child("entry_fields",{"table_name":sub_table_doc_name,"field_name":field.fieldname,"target_field":field.fieldname})
					});
					frm.refresh_field("entry_fields")
				});
				
			});

			frm.refresh_field("entity_fields")
			frm.refresh_field("entry_fields")
		});
	},
});

frappe.ui.form.on('ERP Sync Config Entity Fields', {
	is_key_field(frm,cdt,cdn)
	{
		let fields = frm.doc.entity_fields
		let currentField = fields.filter(function(field){return field.name  == cdn})[0];
		if(currentField.is_key_field == 0)
		{
			return;
		}
		fields.forEach(function(field)
		{
			if(field.name != currentField.name)
			{
				field.is_key_field = 0;
			}

		});
		frm.refresh_field('entity_fields');
	}
	
});

frappe.ui.form.on('ERP Sync Config Entry Fields', {

	
	is_foreign_key(frm,cdt,cdn)
	{
		let fields = frm.doc.entry_fields
		let currentField = fields.filter(function(field){return field.name  == cdn})[0];
		if(currentField.is_foreign_key == 0)
		{
			return;
		}
		fields.filter(function(field){return field.table_name == currentField.table_name}).forEach(function(field)
		{
			if(field.name != currentField.name)
			{
				field.is_foreign_key = 0;
			}

		});
		frm.refresh_field('entry_fields');
	}
	
});
